/**
 * 
 */
package com.scpii.universal.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.AppManageDto;
import com.scpii.universal.dto.EnterMiscroBoDto;
import com.scpii.universal.dto.UserInfoDto;
import com.scpii.universal.hibernate.dao.AppDao;
import com.scpii.universal.hibernate.dao.AppIconDao;
import com.scpii.universal.hibernate.dao.AppManageDao;
import com.scpii.universal.hibernate.dao.EnterMiscroBoDao;
import com.scpii.universal.hibernate.dao.TypeDao;
import com.scpii.universal.hibernate.dao.UserWeiboDao;
import com.scpii.universal.hibernate.entity.AppIconTab;
import com.scpii.universal.hibernate.entity.AppTab;
import com.scpii.universal.hibernate.entity.TypeTab;
import com.scpii.universal.hibernate.entity.UserWeiboTab;
import com.scpii.universal.service.AppManageService;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-28下午02:55:49
 */
@Service
public class AppManageServiceImpl extends
		BaseServiceImpl<AppManageDto, Integer> implements AppManageService {
	@Resource
	private AppManageDao appManageDao;
	@Resource
	private AppDao appDao;
	@Resource
	private AppIconDao appIconDao;
	@Resource
	private EnterMiscroBoDao enterMiscroBoDao;
	@Resource
	private UserWeiboDao userWeiboDao;
	@Resource
	private TypeDao typeDao;

	@Resource
	public void setBaseDao(AppManageDao appManageDao) {
		super.setBaseDao(appManageDao);
	}

	@Override
	public List<AppManageDto> getAppInfo(UserInfoDto userInfo,Integer intAppOsType) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.AppManageDto(t1.intAppId,");
		hql.append(" t1.varAppName , ");
		hql.append(" t1.varAppDescript , ");
		hql.append(" t1.datAppServStarttime , ");
		hql.append(" t1.datAppServEndtime , ");
		hql.append(" t1.dobAppMoney , ");
		hql.append(" t1.intAppDownCount , ");
		hql.append(" t2.intAppOsType , ");
		hql.append(" t4.varIconUrl ,");
		hql.append(" t2.dobAppVersion )");

		// 用用表
		hql.append(" from AppTab t1 , ");
		// 应用更新表
		hql.append("  AppUpdateTab t2 , ");
		// 应用图标更新表
//		hql.append("  AppLoadingUpdateTab t3, ");
		// 应用图标库
		hql.append("  AppIconTab t4 ");

		hql.append(" where t1.intAppId= t2.intAppId ");
//		hql.append(" and t2.intLoadingIconId=t3.intAppUpdateId ");
		hql.append(" and t2.intLoadingIconId=t4.intIconId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");
//		hql.append(" and t3.intDeleteSign=:intDeleteSign ");
		hql.append(" and t4.intDeleteSign=:intDeleteSign ");
		hql.append(" and t1.intAppId=:intAppId ");
		Map<String, Object> argsMap = new HashMap<String, Object>();

		if(intAppOsType!=null){
			hql.append(" and t2.intAppOsType=:intAppOsType ");	
			argsMap.put("intAppOsType", intAppOsType);
		}
		
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		argsMap.put("intAppId", userInfo.getAppId());
		return appManageDao.hqlGetList(hql.toString(), argsMap);

	}

	@Override
	public void updateApp(AppManageDto appManageDto) {

		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());
		// 更新app基本信息
		AppTab tab = appDao.get(appManageDto.getIntAppId());

		tab.setVarAppName(appManageDto.getVarAppName());
		tab.setVarAppDescript(appManageDto.getVarAppDescript());
		tab.setDatAppServEndtime(appManageDto.getDatAppServEndtime());
		tab.setDatAppServStarttime(appManageDto.getDatAppServStarttime());
		tab.setDatUpdateDatetime(nowDate);
		tab.setDobAppMoney(appManageDto.getDobAppMoney());

		appDao.update(tab);

		// 根性loding图标
		if (appManageDto.getVarIconUrl() != null
				&& !"".equals(appManageDto.getVarIconUrl())) {

			AppIconTab icontab = appIconDao.get("intAppId",
					appManageDto.getIntAppId());

			icontab.setVarIconUrl(appManageDto.getVarIconUrl());

			appIconDao.update(icontab);
		}

	}

	@Override
	public void deleteApp(Integer intAppId) {
		// 更新app基本信息
		AppTab tab = appDao.get(intAppId);
		tab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		appDao.update(tab);

	}

	@Override
	public List<EnterMiscroBoDto> getMiscroBoInfo(EnterMiscroBoDto condition,
			Integer pageNo, Integer pageSize) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.EnterMiscroBoDto(t1.intWeiboId, ");
		hql.append(" t1.varWeiboName , ");
		hql.append(" t1.intWeiboType , ");
		hql.append(" t1.appkey , ");
		hql.append(" t1.appsecretkey , ");
		hql.append(" t1.callbackurl , ");
		hql.append(" t2.varAppName ) ");

		hql.append(" from UserWeiboTab t1 , ");
		hql.append("  AppTab t2 ");

		hql.append(" where t1.intAppId= t2.intAppId ");

		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		// 查询条件
		if (condition != null) {
			hql.append(" and t1.intAppId=:intAppId ");
			argsMap.put("intAppId", condition.getIntAppId());
			if (condition.getVarAppName() != null
					&& !"".equals(condition.getVarAppName())) {
				hql.append(" and t2.varAppName like:varAppName");
				argsMap.put("varAppName", "%" + condition.getVarAppName() + "%");
			}

		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);

		return enterMiscroBoDao.findByHql(hql.toString(), pageNo, pageSize,
				argsMap);
	}

	@Override
	public Integer getCountByCondition(EnterMiscroBoDto condition) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.EnterMiscroBoDto(t1.intWeiboId) ");

		hql.append(" from UserWeiboTab t1 , ");
		hql.append("  AppTab t2 ");

		hql.append(" where t1.intAppId= t2.intAppId ");

		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		// 查询条件
		if (condition != null) {
			hql.append(" and t1.intAppId=:intAppId ");
			argsMap.put("intAppId", condition.getIntAppId());
			if (condition.getVarAppName() != null
					&& !"".equals(condition.getVarAppName())) {
				hql.append(" and t2.varAppName like:varAppName");
				argsMap.put("varAppName", "%" + condition.getVarAppName() + "%");
			}

		}
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);

		List<EnterMiscroBoDto> list = enterMiscroBoDao.hqlGetList(
				hql.toString(), argsMap);
		if (list != null) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public EnterMiscroBoDto goEditMiscroBo(Integer intWeibBoId) {
		StringBuffer hql = new StringBuffer(
				"SELECT new com.scpii.universal.dto.EnterMiscroBoDto(t1.intWeiboId, ");
		hql.append(" t1.varWeiboName , ");
		hql.append(" t1.intWeiboType , ");
		hql.append(" t1.appkey , ");
		hql.append(" t1.appsecretkey , ");
		hql.append(" t1.callbackurl , ");
		hql.append(" t2.varAppName ) ");

		hql.append(" from UserWeiboTab t1 , ");
		hql.append("  AppTab t2 ");

		hql.append(" where t1.intAppId= t2.intAppId ");
		hql.append(" and t1.intWeiboId=:intWeiboId ");
		hql.append(" and t1.intDeleteSign=:intDeleteSign ");
		hql.append(" and t2.intDeleteSign=:intDeleteSign ");

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intWeiboId", intWeibBoId);
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<EnterMiscroBoDto> list = enterMiscroBoDao.hqlGetList(
				hql.toString(), argsMap);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void updateWeiBO(EnterMiscroBoDto enterMiscroBoDto) {
		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());

		UserWeiboTab tab = userWeiboDao.get(enterMiscroBoDto.getIntWeiboId());
		tab.setVarWeiboName(enterMiscroBoDto.getVarWeiboName());
		tab.setAppkey(enterMiscroBoDto.getAppkey());
		tab.setAppsecretkey(enterMiscroBoDto.getAppsecretkey());
		tab.setCallbackurl(enterMiscroBoDto.getCallbackurl());
		tab.setIntWeiboType(enterMiscroBoDto.getIntWeiboType());
		tab.setDatUpdateTime(nowDate);
		userWeiboDao.update(tab);

	}

	/**
	 * 获取微博类型
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30下午01:40:39
	 */
	@Override
	public Map<Integer, String> getWeoBoType() {

		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intParentTypeId", GenericConstants.MISCRO_BO_TYPE_ID);
		// 获取微博分类信息
		List<TypeTab> tabList = typeDao.hqlGetList(
				" from TypeTab t where t.intParentTypeId=:intParentTypeId",
				argsMap);
		Map<Integer, String> map = new HashMap<Integer, String>();
		if (tabList != null && tabList.size() > 0) {

			for (TypeTab tab : tabList) {
				map.put(tab.getIntType(), tab.getVarTypeName());
			}
		}

		return map;
	}

	/**
	 * 新增微博信息
	 * 
	 * @param enterMiscroBoDto
	 * @author zhangqiang 2012-7-30下午03:54:48
	 */
	@Override
	public void saveWeiBo(EnterMiscroBoDto enterMiscroBoDto,Integer intAppId) {
		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());

		UserWeiboTab tab = new UserWeiboTab();
		tab.setIntAppId(intAppId);
		tab.setVarWeiboName(enterMiscroBoDto.getVarWeiboName());
		tab.setAppkey(enterMiscroBoDto.getAppkey());
		tab.setAppsecretkey(enterMiscroBoDto.getAppsecretkey());
		tab.setCallbackurl(enterMiscroBoDto.getCallbackurl());
		tab.setIntWeiboType(enterMiscroBoDto.getIntWeiboType());
		tab.setDatUpdateTime(nowDate);
		tab.setIntDeleteSign(GenericConstants.DELETE_SIGN_EXIST);
		userWeiboDao.save(tab);
	}
	/**
	 * 删除微博信息
	 * @param intWeiboId
	 * @author zhangqiang
	 * 2012-7-30下午04:06:21
	 */
	@Override
	public void deleteWeiBO(Integer intWeiboId) {
		SimpleDateFormat sdf = new SimpleDateFormat(
				GenericConstants.DATE_FORMAT);
		String nowDate = sdf.format(new Date());

		UserWeiboTab tab = userWeiboDao.get(intWeiboId);
		tab.setIntDeleteSign(GenericConstants.DELETE_SIGN_DELETED);
		tab.setDatUpdateTime(nowDate);
		userWeiboDao.update(tab);
		
	}

}
